package plgbasev0

import (
	"dgo/goutils/utils"
	"fmt"
	"gitee.com/ymofen/supervalue"
	"time"
)

/*
**

	StatusOutput
	Interval
*/
type IntervalObject struct {
	objdna      uint32
	plug        interface{}
	plugID      string
	topic       string // 状态数据发布
	interval    int
	intervalDNA int
	ReleaseFlag int
}

func NewIntervalObject() *IntervalObject {
	return &IntervalObject{}
}

func (this *IntervalObject) SetPlug(val interface{}) {
	this.plug = val
	if val == nil {
		return
	}
}

func (this *IntervalObject) GetWarningStatus() string {
	if this.plug == nil {
		return fmt.Sprintf("plugid不存在")
	}
	return ""
}

func (this *IntervalObject) CheckDNA() uint32 {
	if this.objdna == 0 {
		this.objdna = utils.NewObjectDNA()
	}
	return this.objdna
}

/*
*

	{
	    "id":"ex",
	    "topic":"xxxx",
	    "interval":1
	 }
*/
func (this *IntervalObject) ReloadConf(conf *wrapper.SuperValue,
	status_fn func(), status_fn_start func(), status_fn_shutdown func()) (err error) {

	if conf == nil {
		return
	}

	conf.CheckStrFunc("id", func(strv string) {
		this.plugID = strv
		ex := FindPluginByID(strv)
		this.SetPlug(ex)
	})
	conf.CheckStrFunc("topic", func(strv string) {
		this.topic = strv
	})

	conf.CheckExistsIntFunc("interval", func(val int64) {
		if this.interval != int(val) {
			this.intervalDNA++
			if this.interval <= 0 { // 之前关闭
				if status_fn_start != nil {
					status_fn_start()
				}
			}
			this.interval = int(val)
			if this.interval > 0 {
				dna := this.intervalDNA
				this.CheckDNA()
				utils.AddTaskFxiedInterval(fmt.Sprintf("obj_%d", this.objdna),
					time.Duration(this.interval)*time.Second, func() bool {
						if dna != this.intervalDNA || this.ReleaseFlag == 1 {
							if status_fn_shutdown != nil {
								status_fn_shutdown()
							}
							return false
						}
						if status_fn != nil {
							status_fn()
						}
						return true
					})
			} else {
				if status_fn_shutdown != nil {
					status_fn_shutdown()
				}
			}

		}

	})
	return
}

func (this *IntervalObject) SendStatus(msg string) {
	ex := this.plug
	if ex == nil {
		ex = FindPluginByID(this.plugID)
		if ex != nil {
			this.plug = ex
		}
	}
	if ex == nil {
		return
	}

	if sc, ok := ex.(IDispatchData); ok {
		sc.DispatchData(this.topic, this, msg, nil)
	} else if sc, ok := ex.(IGetSubscriber); ok {
		sc.GetSubscriber().DispatchData(this.topic, this, msg, nil)
	} else if sc, ok := ex.(utils.SubscribeCenter); ok {
		sc.DispatchData(this.topic, this, msg, nil)
	} else if sc, ok := ex.(ISendKVs); ok {
		sc.SendKVs(this.topic, msg)
	}
}

func (this *IntervalObject) SendStatusFunc(fillStatus func(json *wrapper.SuperValue)) error {
	ex := this.plug
	if ex == nil {
		ex = FindPluginByID(this.plugID)
		if ex != nil {
			this.plug = ex
		}
	}
	if ex == nil {
		return fmt.Errorf("[%s]插件不存在", this.plugID)
	}
	if sc, ok := ex.(IDispatchData); ok {
		json := wrapper.NewSVObject()
		fillStatus(json)
		return sc.DispatchData(this.topic, this, json, nil)
	} else if sc, ok := ex.(IGetSubscriber); ok {
		json := wrapper.NewSVObject()
		fillStatus(json)
		if sc.GetSubscriber().DispatchData(this.topic, this, json, nil) <= 0 {
			return fmt.Errorf("推送[%s]消息失败", this.topic)
		}
		return nil
	} else if sc, ok := ex.(utils.SubscribeCenter); ok {
		json := wrapper.NewSVObject()
		fillStatus(json)
		if sc.DispatchData(this.topic, this, json, nil) <= 0 {
			return fmt.Errorf("推送[%s]消息失败", this.topic)
		}
		return nil
	} else if sc, ok := ex.(ISendKVs); ok {
		json := wrapper.NewSVObject()
		fillStatus(json)
		return sc.SendKVs(this.topic, json)
	}
	return fmt.Errorf("[%s]不支持推送", this.plugID)
}
